package licode.unisop.client.utils;

import licode.unisop.client.api.SopPlatformAuth;
import licode.unisop.client.info.SopIdInfo;
import licode.unisop.client.vo.SopAuthPlatform;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 检测平台内部令牌
 *
 * @author licode
 * @version 1.0
 * @date 2016-08-01 11:03
 */
@Component
public class PlatformCheck {
    @Resource
    private SopPlatformAuth sopPlatformAuth;

    public void checkPlatform(HttpServletRequest request) {
        String platId = SopRequestParse.getPlatformId(request);
        SopAuthPlatform authPlat = sopPlatformAuth.fromId(SopIdInfo
                .builder().id(platId).build());
        if (null == authPlat) {
            throw new SopException(-1, "无效平台");
        }
    }

    public void checkModalToken(HttpServletRequest request) {
        String platId = SopRequestParse.getPlatformId(request);
        String modalToken = SopRequestParse.getModalToken(request);
        SopAuthPlatform authPlat = sopPlatformAuth.fromId(SopIdInfo
                .builder().id(platId).build());
        if (null == authPlat) {
            throw new SopException(-1, "无效平台");
        }
        if (StringUtil.isNotBlank(modalToken) && !authPlat.getModelToken().equals(modalToken)) {
            throw new SopException(-1, "平台未认证");
        }
    }
}
